package org.sum.prictice;

import java.util.Random;

import org.junit.Test;

public class CardPrictice {
	public static void main(String[] args) {
		Random ran = new Random();
		int[] ar = new int[6];
		for (int i = 0; i < 6; i++) {
			ar[i] = ran.nextInt(10);
		}
		System.out.print("未排序之前的数：");
		for (int i : ar) {
			System.out.print(i + "　");
		}
		System.out.println();
		quickSort(ar, 0, ar.length - 1);
		System.out.print("使用快速排序之后：");
		for (int i : ar) {
			System.out.print(i + "　");
		}
		System.out.println();
		int trip = 0;
		int[] tri = Tripite(ar);
		for (int i = 0; i < 2; i++) {
			if (tri[i] != 0) {
				trip++;
			}
		}
		if (trip == 2) {
			System.out.println("数字结果为：baby-gain！！");
			return;
		}
		int run = 0;
		int[] ru = run(ar);
		for (int i = 0; i < 2; i++) {
			if (ru[i] != 0) {
				run++;
			}
		}
		if (run == 2) {
			System.out.println("数字结果为：baby-gain！！");
			return;
		}
		if (run == 1 && trip == 1) {
			System.out.println("数字结果为：baby-gain！！");
			return;
		}
		if (trip == 1) {
			System.out.println("数字结果为：trip！！");
			return;
		}
		if (run == 1) {
			System.out.println("数字结果为：run！！");
			return;
		}
		System.out.println("什么都不是");
	}

	public void Test() {
		int[] ar = { 1, 2, 3, 3, 4, 5 };
		int[] run = run(ar);
		System.out.print("run的结果为：");
		for (int i : run) {
			System.out.print(i + "　");
		}
		System.out.println();
	}

	public static int[] Tripite(int[] ar) {
		int temp = ar[0];
		int flag = 1;
		int[] tri = new int[2];
		int j = 0;
		for (int i = 1; i < ar.length; i++) {
			if (ar[i] == temp) {
				flag++;
			} else {
				temp = ar[i];
				flag = 1;
			}
			if (flag == 3) {
				tri[j++] = i;
				flag = 1;
			}
		}
		if (tri[0] != 0) {
			ar[tri[0]] = -1;
			ar[tri[0] - 1] = -1;
			ar[tri[0] - 2] = -1;
		}
		if (tri[1] != 0) {
			ar[tri[1]] = -1;
			ar[tri[1] - 1] = -1;
			ar[tri[1] - 2] = -1;
		}
		return tri;
	}

	// 实现run的判定
	public static int[] run(int[] ar) {
		int[] ru = new int[2];
		int[] ne = new int[6];
		int[] ne2 = new int[6];
		ne = reduce(ar);
		System.out.print("新建的数组为ne：");
		for (int i : ne) {
			System.out.print(i + " ");
		}
		System.out.println();
		int j = 0;
		int[] ru1 = chechSort(ne);
		for (int i = 0; i < ru1.length && ru1[i] != 0; i++) {
			ru[j++] = ru1[i];
		}
		if (ru[1] != 0) {
			return ru;
		}
		int temp = -3;
		if (ru[0] != 0) {
			temp = ne[ru[0]] - 2;
		}
		if (temp != -3) {
			int flag = 0;
			for (int i = 0; i < ar.length; i++) {
				if (ar[i] == temp) {
					ar[i] = -1;
					temp++;
					flag++;
				}
				if (flag == 3) {
					break;
				}
			}
			ne2 = reduce(ar);
			System.out.print("新建的数组为ne2：");
			for (int i : ne2) {
				System.out.print(i + " ");
			}
			System.out.println();
			int[] ru2 = chechSort(ne2);
			for (int i = 0; ru2[i] != 0; i++) {
				ru[j++] = ru2[i];
			}
		}
		return ru;
	}

	// 去掉-1
	public static int[] reduce(int[] ar) {
		int[] ne = new int[6];
		ne[0] = -2;
		for (int i = 0, j = 0; i < ar.length; i++) {
			if (ar[i] != -1) {
				if (i == 0) {
					ne[j++] = ar[0];
				}
				if (i > 0 && ar[i] != ar[i - 1]) {
					ne[j++] = ar[i];
				}
			}
		}
		return ne;
	}

	// 创建一个判定数组顺序的方法
	public static int[] chechSort(int[] ne) {
		int[] ru = new int[2];
		if (ne[0] == -2) {
			return ru;
		}
		int temp = ne[0];
		int flag = 1;
		for (int i = 1, j = 0; i < ne.length && ne[i] != 0; i++) {
			if (ne[i] == temp) {
				continue;
			}
			if (ne[i] == ++temp) {
				flag++;
			} else {
				temp = ne[i];
				flag = 1;
			}
			if (flag == 3) {
				ru[j++] = i;
				flag = 0;
			}
		}
		return ru;
	}

	// 实现排序排序的函数
	public static void quickSort(int[] ar, int low, int high) {
		if (low < high) {
			int index = quickView(ar, low, high);
			quickSort(ar, low, index - 1);
			quickSort(ar, index + 1, high);
		}
	}

	public static int quickView(int[] ar, int low, int high) {
		// 将low的数存储起来，最后的时候使用
		int temp = ar[low];
		// 进行扫描
		while (low < high) {
			while (low < high && ar[high] >= temp) {
				high--;
			}
			ar[low] = ar[high];
			while (low < high && ar[low] <= temp) {
				low++;
			}
			ar[high] = ar[low];
		}
		ar[low] = temp;
		return low;
	}
}
